#include "LUA_Images.h"
#include "LUA_util.h"

int loadImage(lua_State* L){	
	const char* imgPath = lua_tostring(L, -1);
	//lua_pop(L,1);

	ILuint img;
	GLuint tex;
	int width;
	int height;

	ilGenImages(1, &img);
	ilBindImage(img);
	ilLoadImage(imgPath);

	ILuint devilError = ilGetError();
	if(devilError != IL_NO_ERROR){		
		if(devilError == IL_COULD_NOT_OPEN_FILE)
			LUAERROR("could not open File");
		else LUAERROR("an error occured while loading an image");
		SDL_Quit();
	}

	width = ilGetInteger(IL_IMAGE_WIDTH);
	height = ilGetInteger(IL_IMAGE_HEIGHT);

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
      ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), IL_UNSIGNED_BYTE,
      ilGetData());	

	ilDeleteImages(1, &img);

	luaL_checktype(L, 1, LUA_TTABLE); 
	lua_newtable(L);
	lua_pushvalue(L,1);       
    lua_setmetatable(L, -2);
	lua_pushvalue(L,1);
    lua_setfield(L, 1, "__index");

	imageData* imgData = (imageData*)lua_newuserdata(L, sizeof(imageData));	
	imgData->posX = 0;
	imgData->posY = 0;
	imgData->width = width;
	imgData->height = height;
	imgData->rotation = 0;
	imgData->glTex = tex;


	lua_getglobal(L, "Data.Image");	
	lua_setmetatable(L, -2);
	lua_setfield(L, -2, "__self");

	return 1;
}

int drawPosImage(lua_State* L){
	luaL_checktype(L, 1, LUA_TTABLE);
	lua_getfield(L, 1, "__self");
	imageData* img = (imageData*)lua_touserdata(L, -1);
	luaL_argcheck(L, img!=0,-1, "Data.Image' expected"); 

	float posY = lua_tonumber(L,-2);
	float posX = lua_tonumber(L,-3);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, img->glTex);

	glLoadIdentity();
	glTranslatef(posX, posY, 0.0f);

	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);
    glTexCoord2i(0, 0); glVertex2i(0,   0);
	glTexCoord2i(0, 1); glVertex2i(0,   img->height);
	glTexCoord2i(1, 1); glVertex2i(img->width, img->height);
    glTexCoord2i(1, 0); glVertex2i(img->width, 0);
    glEnd();	

	return 0;
}

int drawImage(lua_State* L){
	luaL_checktype(L, 1, LUA_TTABLE);
	lua_getfield(L, 1, "__self");
	imageData* img = (imageData*)lua_touserdata(L, -1);
	luaL_argcheck(L, img!=0,-1, "Data.Image' expected");

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, img->glTex);

	glLoadIdentity();
	glTranslatef(img->posX, img->posY, 0.0f);

	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);
    glTexCoord2i(0, 0); glVertex2i(0,   0);
	glTexCoord2i(0, 1); glVertex2i(0,   img->height);
	glTexCoord2i(1, 1); glVertex2i(img->width, img->height);
    glTexCoord2i(1, 0); glVertex2i(img->width, 0);
    glEnd();

	return 0;
}

int resizeImage(lua_State* L){	
	luaL_checktype(L, 1, LUA_TTABLE);
	lua_getfield(L, 1, "__self");
	imageData* img = (imageData*)lua_touserdata(L, -1);
	luaL_argcheck(L, img!=0,-1, "Data.Image' expected");
	
	img->height = lua_tointeger(L,-2);
	img->width = lua_tointeger(L,-3);	

	return 0;
}

int drawPosSubImage(lua_State* L){

	double texHeight = lua_tonumber(L,-1);
	double texWidth = lua_tonumber(L,-2);
	double texPosY = lua_tonumber(L, -3);
	double texPosX = lua_tonumber(L, -4);
	double posY = lua_tonumber(L, -5);
	double posX = lua_tonumber(L, -6);

	imageData* img = (imageData*)getClass(L, "Data.Image");

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, img->glTex);

	glLoadIdentity();
	glTranslatef(posX, posY, 0.0f);

	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);
	glTexCoord2d(texPosX, texPosY); glVertex2i(0,   0);
	glTexCoord2d(texPosX, texPosY+texHeight); glVertex2i(0,   img->height);
	glTexCoord2d(texPosX+texWidth, texPosY+texHeight); glVertex2i(img->width, img->height);
	glTexCoord2d(texPosX+texWidth, texPosY); glVertex2i(img->width, 0);
	glEnd();

	return 0;
}

int drawSubImage(lua_State* L){

	double texHeight = lua_tonumber(L,-1);
	double texWidth = lua_tonumber(L,-2);
	double texPosY = lua_tonumber(L, -3);
	double texPosX = lua_tonumber(L, -4);

	imageData* img = (imageData*)getClass(L, "Data.Image");

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, img->glTex);

	glLoadIdentity();
	glTranslatef(img->posX, img->posY, 0.0f);

	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);
	glTexCoord2d(texPosX, texPosY); glVertex2i(0,   0);
	glTexCoord2d(texPosX, texPosY+texHeight); glVertex2i(0,   img->height);
	glTexCoord2d(texPosX+texWidth, texPosY+texHeight); glVertex2i(img->width, img->height);
	glTexCoord2d(texPosX+texWidth, texPosY); glVertex2i(img->width, 0);
	glEnd();

	return 0;
}

int setPosImage(lua_State* L){
	int x = lua_tointeger(L, -2);
	int y = lua_tointeger(L, -1);

	imageData* img = (imageData*)getClass(L, "Data.Image");

	img->posX = x;
	img->posY = y;

	return 0;
}

int getSizeImage(lua_State* L){
	imageData* img = (imageData*)getClass(L, "Data.Image");
	lua_pushinteger(L, img->width);
	lua_pushinteger(L, img->height);
	return 2;
}

//only works if position is set with "setPosition"
int isMouseOverImage(lua_State* L){
	imageData* img = (imageData*)getClass(L, "Data.Image");

	int mouseX;
	int mouseY;
	SDL_GetMouseState(&mouseX, &mouseY);

	if(mouseX > (img->posX) && mouseX < (img->posX)+(img->width) && mouseY > (WINDOWSIZE_Y-(img->posX+img->height)) && mouseY < WINDOWSIZE_Y - img->posY)
		lua_pushinteger(L, true);
	else lua_pushinteger(L, false);

	return 1;
}



